import { Reflector, RouterModule } from "@nestjs/core";
import * as Job_User from '@/modules/workstation/job/api/user/user.module'
import * as Job_Common from '@/modules/workstation/job/api/common/common.module'
import * as Job_Manager from '@/modules/workstation/job/api/manager/manager.module'
import * as Job_Agent from '@/modules/workstation/job/api/agent/agent.module'
import * as Common_Common from '@/modules/workstation/common/common.module'
import * as Typecho_Common from '@/modules/workstation/typecho/typecho.module'

import { UseGuards } from "@nestjs/common";
import { AuthGuard } from "./SHA256_token_auth.guard";
import { WechatGatewayModule } from '@/modules/gateway/gateway.module'

export function registerRouter() {
    // ['路由路径',[授权角色...],模块]
    const routers = [
        ['common', null, Common_Common.CommonModule],
        ['common/job', null, Job_Common.CommonModule],
        ['user/job', [], Job_User.UserModule],
        ['agent/job', [], Job_Agent.AgentModule],
        ['manager/job', [], Job_Manager.ManagerModule],
        ['common/typecho', null, Typecho_Common.TypechoModule],
        ['wechat', [], WechatGatewayModule, ['GeweReceiverController']]
    ]

    routers.forEach(router => {
        if (router[1]) {
            let controllers = Reflect.getMetadata('controllers', router[2]);
            //给每个模块的控制器自动添加授权守卫
            controllers.forEach(controller => {
                if (router[3] && controller) {
                    let exclueds = router[3] as []
                    let excluded = exclueds.findIndex(e=>e == controller.name) as any
                    
                    if(excluded > -1)
                        return
                }
                Reflect.decorate([UseGuards(AuthGuard)], controller);
            })
        }
    })

    let totals: any = routers.map(r => ({ path: r[0], module: r[2] }));
    let modules: any = routers.map(r => r[2]);

    return [...modules, RouterModule.register(totals)]
}